package br.com.manutencao.repositories;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;

import javax.persistence.EntityManager;
import javax.persistence.Persistence;
import javax.persistence.Query;

import br.com.manutencao.entities.Maquina;

public class MaquinaRepository implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	
	private EntityManager em = Persistence.createEntityManagerFactory("jsf")
			.createEntityManager();

	public void adiciona(Maquina maquina) {
		em.getTransaction().begin();
		em.persist(maquina);
		em.getTransaction().commit();
	}
	
	public void altera(Maquina maquina){
		em.getTransaction().begin();
		em.merge(maquina);
		em.getTransaction().commit();
	}
	
	public void delete(Maquina maquina){
		em.getTransaction().begin();
		em.remove(maquina);
		em.getTransaction().commit();
	}

	@SuppressWarnings("all")
	public Collection<Maquina> buscaTodos(){
		Collection<Maquina> maquinas = new ArrayList<Maquina>();
		
		em.getTransaction().begin();
		Query query = em.createNamedQuery("Maquina.findAll");
		maquinas = query.getResultList();
		em.getTransaction().commit();
		
		return maquinas;
	}
	
	public Maquina buscaPorId(int id){
		em.getTransaction().begin();
		Maquina maquina = em.find(Maquina.class, id);
		em.getTransaction().commit();
		
		return maquina;
	}
}
